#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > cc_prop_file.info <<'%EOF%'
   cc_prop_file
   -------------
   Molecule:         CH2O
   Wave Function:    CCSD / STO-3G  
   Test Purpose:     Test if correct write to DALTON.PROP from CC module
%EOF%
#######################################################################
#  INTEGRAL INPUT
#######################################################################
cat > cc_prop_file.mol <<'%EOF%'
ATOMBASIS
CH2O
------------------------
    3              1 1.00D-12
        6.0   1    Basis=STO-3G
C           -1.588367    -.770650     .029109
        8.0   1    Basis=STO-3G
O           -1.657083     .436069    -.009750
        1.0   2    Basis=STO-3G
H           -.620668   -1.294822      .054251
H           -2.508043   -1.382001     .040282
%EOF%
#######################################################################
#  DALTON INPUT
#######################################################################
cat > cc_prop_file.dal <<'%EOF%'
**DALTON
.RUN WAVEFUNCTION
**INTEGRALS
.DIPLEN
**WAVE FUNCTIONS
.CC
*SCF INPUT
.THRESH
1.0D-12
.PRINT
 3
*CC INP
.CCSD
.FREEZE
 2 0
.THRLEQ
 1.0D-12
.THRENR
 1.0D-12
.PRINT
 2
.MAX IT
 600
.MAXRED
 600
.MXLRV
 200
*CCFOP
.DIPMOM
.NONREL
*CCLR
.DIPOLE
*CCEXCI
.NCCEXCI
 2
**END OF DALTON INPUT
%EOF%
#
#######################################################################
#  CHECK SCRIPT
#######################################################################
#
echo $CHECK_SHELL >cc_prop_file.check
cat >>cc_prop_file.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

#
# Test af Energies
#

CRIT1=`$GREP "#  1 1 ENERGY   Ground state Energy = * \-112\.49488640" $log | wc -l`
TEST[1]=` expr $CRIT1 ` 
CTRL[1]=1
ERROR[1]="ENERGY NOT CORRECTLY WRITTEN TO PROPERTY FILE"

#
# Test of CCSD dipole moment 
#
CRIT1=`$GREP "#  2 1 * <XDIPLEN > =   *  \-0*\.00000755" $log | wc -l`
CRIT2=`$GREP "#  3 1 * <YDIPLEN > =   *    0*\.00376264" $log | wc -l`
CRIT3=`$GREP "#  4 1 * <ZDIPLEN > =   *  \-0*\.44399835" $log | wc -l`
TEST[2]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 `
CTRL[2]=3
ERROR[2]="CCSD DIPOLE MOMENT NOT CORRECTLY WRITTEN TO PROPERTY FILE"

#
# Test of CCSD polarizability
#
CRIT1=`$GREP " #  9 1 * <<XDIPLEN ,XDIPLEN >>\( ( |0)\.000000\) =  *    2\.465612" $log | wc -l`
CRIT2=`$GREP " # 10 1 * <<XDIPLEN ,YDIPLEN >>\( ( |0)\.000000\) =  *   0*\.00000" $log | wc -l`
CRIT3=`$GREP " # 11 1 * <<XDIPLEN ,ZDIPLEN >>\( ( |0)\.000000\) =  *   0*\.00005" $log | wc -l`
CRIT4=`$GREP " # 12 1 * <<YDIPLEN ,XDIPLEN >>\( ( |0)\.000000\) =  *   0*\.00000" $log | wc -l`
CRIT5=`$GREP " # 13 1 * <<YDIPLEN ,YDIPLEN >>\( ( |0)\.000000\) =  *    6\.074557" $log | wc -l`
CRIT6=`$GREP " # 14 1 * <<YDIPLEN ,ZDIPLEN >>\( ( |0)\.000000\) =  * \-0*\.023016" $log | wc -l`
CRIT7=`$GREP " # 15 1 * <<ZDIPLEN ,XDIPLEN >>\( ( |0)\.000000\) =  *   0*\.00005" $log | wc -l`
CRIT8=`$GREP " # 16 1 * <<ZDIPLEN ,YDIPLEN >>\( ( |0)\.000000\) =  * \-0*\.023016" $log | wc -l`
CRIT9=`$GREP " # 17 1 * <<ZDIPLEN ,ZDIPLEN >>\( ( |0)\.000000\) =  *    8\.604065" $log | wc -l`
TEST[3]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ $CRIT7 \+ $CRIT8 \+ $CRIT9 `
CTRL[3]=9
ERROR[3]="CCSD POLARZABILITY NOT CORRECTLY WRITTEN TO PROPERTY FILE"

#
# Test of excited state energies
#
CRIT1=`$GREP " #  5 1 Sy\: 1 Sp\: 1 Exc\:  1 E_tot\: *  \-112\.344675[89]" $log | wc -l`
CRIT2=`$GREP " #  6 1 Exctot   Excited state Energy =    \-112\.344675[89]" $log | wc -l`
CRIT3=`$GREP " #  7 1 Sy: 1 Sp: 1 Exc:  2 E_tot:   \-112\.126928." $log | wc -l`
CRIT4=`$GREP " #  8 1 Exctot   Excited state Energy =    \-112\.126928." $log | wc -l`
TEST[4]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 `
CTRL[4]=4
ERROR[4]="CCSD EXCITED STATE ENERGIES NOT CORRECTLY WRITTEN TO PROPERTY FILE"

PASSED=1 
for i in 1 2 3 4
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi                
%EOF%
chmod +x cc_prop_file.check
#######################################################################
